﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;
using TorrentService;
using log4net;
using log4net.Config;
using System.Net;
using XmlSettings;

namespace BotTorrent
{
    public class BotTorrent : ServiceBase
    {
        // Fields
        public ircbot Bot;
        public BitTorrentClient BTClient;
        public Thread IrcThread;
        private static readonly ILog log = LogManager.GetLogger(typeof(BotTorrent));
        public RssFeedManager RssManager;
        public Settings Settings;

        // Methods
        private static void Main(string[] args)
        {
            BotTorrent service = new BotTorrent();
            if (Environment.UserInteractive)
            {
                service.OnStart(args);
                Console.WriteLine("Press any key to stop program");
                Console.ReadKey();
                service.OnStop();
            }
            else
            {
                ServiceBase.Run(service);
            }
        }

        protected override void OnStart(string[] args)
        {
            BasicConfigurator.Configure();
            log.Info("Log4net initialised with basic settings");
            log.Debug("Loading Settings");
            this.Settings = Settings.Load("torrentconfig.xml");
            this.WaitForInternetConnection();
            this.BTClient = new BitTorrentClient(this.Settings, this.Bot);
            this.Bot = new ircbot();
            this.Bot.Settings = this.Settings;
            this.Bot.BTClient = this.BTClient;
            new Thread(new ThreadStart(this.Bot.Initialise)).Start();
            new WebUI(this.BTClient).StartService();
            this.RssManager = new RssFeedManager();
            this.RssManager.IrcBot = this.Bot;
            this.RssManager.Settings = this.Settings;
            this.RssManager.BTClient = this.BTClient;
            new Thread(new ThreadStart(this.RssManager.Start)).Start();
        }

        protected override void OnStop()
        {
            this.BTClient.shutdown();
            if (this.Bot.IsConnected)
            {
                this.Bot.Disconnect();
            }
            if (Environment.UserInteractive)
            {
                Environment.Exit(1);
            }
        }

        private void WaitForInternetConnection()
        {
            bool isConnected = true;
            Uri objUrl = new Uri("http://www.google.com/");
            while (!isConnected)
            {
                try
                {
                    WebRequest.Create(objUrl).GetResponse();
                    log.Info("Internet connection detected, starting up");
                    isConnected = true;
                }
                catch (Exception)
                {
                    log.Debug("No internet connection detected, sleeping for 5 seconds");
                    Thread.Sleep(0x1388);
                }
            }
        }
    }


}
